# -*- coding:utf-8 -*-
import datetime
import os
import random
import time

from PIL import Image, ImageDraw, ImageFont

import subprocess

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
"""

@author: liandyao
@date: 2025/1/16 17:45
"""
class AutoRelease:
    def __init__(self):
        pass

    # 快手发布
    '''
    video_path 表示视频的路径
    title 表示需要写在封面的标题
    account_en 是账号的英文名称或者数字,不要使用中文
    '''
    def auto_release_kuaishou(self,account_en):
        # 创建 ChromeOptions，并指定 chrome.exe 的路径
        # 获取当前工作目录
        base_dir = os.getcwd()

        # 指定一个新的用户数据目录
        new_user_data_directory = os.path.join(base_dir, f"{account_en}")
        if not os.path.exists(new_user_data_directory):
            os.makedirs(new_user_data_directory)

        # 创建 ChromeOptions
        options = webdriver.ChromeOptions()
        options.add_argument(f"user-data-dir={new_user_data_directory}")  # 使用新的用户数据目录
        options.add_argument("profile-directory=Default")
        options.add_argument("--start-maximized")  # 最大化窗口

        # 启动 Chrome
        driver = webdriver.Chrome(options=options)

        try:
            driver.get("https://cp.kuaishou.com/article/publish/video")
        except Exception as e:
            print(f"Failed to load page: {e}")

        time.sleep(3000)



        driver.quit()  # 确保在完成后关闭浏览器


    def auto_release_douyin(self,account_en):

        # 创建 ChromeOptions，并指定 chrome.exe 的路径
        # 获取当前工作目录
        base_dir = os.getcwd()

        # 指定一个新的用户数据目录
        new_user_data_directory = os.path.join(base_dir, f"{account_en}")
        if not os.path.exists(new_user_data_directory):
            os.makedirs(new_user_data_directory)

        # 创建 ChromeOptions
        options = webdriver.ChromeOptions()
        options.add_argument(f"user-data-dir={new_user_data_directory}")  # 使用新的用户数据目录
        options.add_argument("profile-directory=Default")
        options.add_argument("--start-maximized")  # 最大化窗口

        # 启动 Chrome
        driver = webdriver.Chrome(options=options)

        '''
             作用：发布抖音视频
            '''

        # 进入创作者页面，并上传视频
        driver.get("https://creator.douyin.com/creator-micro/content/manage")
        time.sleep(3)
        for i in range(1, 110):
            # 找到div class=op-btn-ILGveS的元素
            try:

                delete_button = WebDriverWait(driver, 10).until(
                    EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.op-btn-ILGveS"))
                )
                # 点击删除按钮
                print(delete_button)
                delete_button[5].click()

            except Exception as e:
                print(f"Failed to click delete_button : {e}")

            # 找到button class=primary-cECiOJ的按钮并点击
            try:
                del_ok_button = WebDriverWait(driver, 600).until(
                    EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'primary-cECiOJ')]"))
                )
                # 点击确认按钮
                del_ok_button.click()
            except Exception as e:
                print(f"Failed to click publish_button : {e}")

            # 等待 10 秒
            time.sleep(6)

        driver.quit()  # 确保在完成后关闭浏览器
    def auto_release_toutiao(self,account_en):

        # 创建 ChromeOptions，并指定 chrome.exe 的路径
        # 获取当前工作目录
        base_dir = os.getcwd()

        # 指定一个新的用户数据目录
        new_user_data_directory = os.path.join(base_dir, f"{account_en}")
        if not os.path.exists(new_user_data_directory):
            os.makedirs(new_user_data_directory)

        # 创建 ChromeOptions
        options = webdriver.ChromeOptions()
        options.add_argument(f"user-data-dir={new_user_data_directory}")  # 使用新的用户数据目录
        options.add_argument("profile-directory=Default")
        options.add_argument("--start-maximized")  # 最大化窗口

        # 启动 Chrome
        driver = webdriver.Chrome(options=options)

        '''
             作用：发布抖音视频
            '''

        # 进入创作者页面，并上传视频
        driver.get("https://mp.toutiao.com/profile_v4/manage/content/all")
        time.sleep(6)
        for i in range(1, 180):
            # 找到div class=op-btn-ILGveS的元素
            try:

                delete_buttons = WebDriverWait(driver, 10).until(
                    EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.item-inner"))
                )

                # 在获取到的元素集合中查找包含"更多"的元素
                target_div = next((btn for btn in delete_buttons if "更多" in btn.text), None)
                # 点击删除按钮
                print(target_div)
                target_div.click()

            except Exception as e:
                print(f"Failed to click 1 : {e}")
            time.sleep(1)
            # 找到div class=zoomTooltip-enter-done的元素并点击
            try:

                del_ok_button = WebDriverWait(driver, 10).until(
                    EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.item-inner"))
                )

                # 在获取到的元素集合中查找包含"更多"的元素
                target_div = next((btn for btn in del_ok_button if "删除作品" in btn.text), None)
                print('删除按钮', target_div.text)
                # 点击确认按钮
                target_div.click()
            except Exception as e:
                print(f"Failed to click 2 : {e}")

            time.sleep(1)
            # 找到button class=primary-cECiOJ的按钮并点击
            try:
                del_ok_button = WebDriverWait(driver, 10).until(
                    EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "button.byte-btn-primary"))
                )
                print('确认按钮',del_ok_button)
                target_div = next((btn for btn in del_ok_button if "确定" in btn.text), None)
                print('确认按钮', target_div)
                # 点击确认按钮
                target_div.click()
            except Exception as e:
                print(f"Failed to click 3 : {e}")

            # 等待 10 秒
            time.sleep(6)

        driver.quit()  # 确保在完成后关闭浏览器

if __name__ == '__main__':
    #AutoRelease().auto_release_kuaishou('User_Data_ks_yingyu1')
    #AutoRelease().auto_release_kuaishou('User_Data_ks_bannichengzhang')
    #AutoRelease().auto_release_kuaishou('User_Data_ks_anxinshuijiao')
    #AutoRelease().auto_release_douyin('User_Data')
    AutoRelease().auto_release_toutiao('User_Data_dy_baozi')